//
//  UIImage+TYGIF.m
//  UnionMedApp
//
//  Created by 谢航宇_WORK on 2021/4/8.
//  Copyright © 2021 唐家钦. All rights reserved.
//

#import "UIImage+TYGIF.h"

@implementation UIImage (TYGIF)
+ (NSArray *)getImageFromGifName:(NSString *)gifName {
    NSMutableArray *imageArray = [NSMutableArray array];
    
    // 获取gif url
    NSURL *url = [[NSBundle mainBundle] URLForResource:gifName withExtension:@"gif"];
    // 转换为图片源
    CGImageSourceRef gifImageSourceRef = CGImageSourceCreateWithURL((CFURLRef)url, nil);
    // 获取图片个数
    size_t framesCount = CGImageSourceGetCount(gifImageSourceRef);
    for (size_t i = 0; i < framesCount; i++) {
        CGImageRef imageRef = CGImageSourceCreateImageAtIndex(gifImageSourceRef, i, nil);
        UIImage *image = [UIImage imageWithCGImage:imageRef];
        
        [imageArray addObject:image];
        
        CFRelease(imageRef);
    }
    return imageArray;
}

//获取动图

+ (UIImage *)animatedGIFWithData:(NSData *)data {
    
    if(!data) {
        
        return nil;
        
    }
    //获取动图源数据
    
    CGImageSourceRef source = CGImageSourceCreateWithData((__bridge CFDataRef)data, NULL);
    
    //获取动图图片集数量
    
    size_t count = CGImageSourceGetCount(source);
    
    UIImage * animatedImage;
    
    //如果图片集只有一张，那么直接创建成一张图片，否则合成一个动图
    
    if(count <=1) {
        
        animatedImage = [[UIImage alloc]initWithData:data];
        
    }else {
        
        NSMutableArray * images = [NSMutableArray array];
        
        //记录动图总时长
        
        NSTimeInterval duration =0.0f;
        
        //遍历每张图片
        
        for(size_t i =0; i < count; i++) {
            
            CGImageRef image =CGImageSourceCreateImageAtIndex(source, i,NULL);
            
            //获取单张动图时间间距
            
            duration += [self frameDurationAtIndex:i source:source];
            
            [images addObject:[UIImage imageWithCGImage:image scale:[UIScreen mainScreen].scale orientation:UIImageOrientationUp]];
            
            CGImageRelease(image);
            
        }
        
        //若未获取到时长，，自定义时长为0.1x张数，，这里可以根据需求自定义
        
        if(!duration) {
            duration = (1.0f/10.0f) * count;
        }
        
        //创建动图
        animatedImage = [UIImage animatedImageWithImages:images duration:duration];
    }
    CFRelease(source);
    
    return animatedImage;
    
}
//计算动图时间间距
+ (float)frameDurationAtIndex:(NSUInteger)index source:(CGImageSourceRef)source {

    float frameDuration =0.1f;

    CFDictionaryRef cfFrameProperties = CGImageSourceCopyPropertiesAtIndex(source, index,nil);

    NSDictionary * frameProperties = (__bridge NSDictionary*)cfFrameProperties;

    NSDictionary * gifProperties = frameProperties[(NSString*)kCGImagePropertyGIFDictionary];

    NSNumber * delayTimeUnclampedProp = gifProperties[(NSString*)kCGImagePropertyGIFUnclampedDelayTime];

    if(delayTimeUnclampedProp) {

        frameDuration = [delayTimeUnclampedProp floatValue];

    }else {

        NSNumber * delayTimeProp = gifProperties[(NSString*)kCGImagePropertyGIFDelayTime];

        if(delayTimeProp) {

            frameDuration = [delayTimeProp floatValue];

        }

    }

    CFRelease(cfFrameProperties);

    return frameDuration;

}
 
@end
